package com.paic.egis.smts.common.util;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.RandomUtils;

public class DateUtil {

	public final static String className=String.valueOf(DateUtil.class.getName());
	
	public final static String PATTERN = "yyyy-MM-dd";

	public final static String PATTERN_TIME = "yyyy-MM-dd HH:mm:ss";

	public static Date parseString(String date, String format) throws ParseException {

		SimpleDateFormat dateformat = new SimpleDateFormat(format);
		Date timestamp = dateformat.parse(date);

		return timestamp;
	}

	public static String parseDate(Date date, String format) {
		if (date == null) {
			return null;
		}
		SimpleDateFormat dateformat = new SimpleDateFormat(format);
		String timestamp = dateformat.format(date);
		return timestamp;
	}

	public static String payPremiumToYuan(String payPremium) {
		BigDecimal bdl = BigDecimal.valueOf(Integer.valueOf(payPremium));
		String result = String.valueOf(bdl.multiply(BigDecimal.valueOf(0.01)));
		return result;
	}

	/**
	 * 获取本周第一天
	 * 
	 * @return
	 */
	public static String getFirstDayOfWeek() {
		Calendar c = Calendar.getInstance(Locale.CHINA);
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTimeInMillis(System.currentTimeMillis());
		c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return new SimpleDateFormat(PATTERN).format(c.getTime());
	}

	/**
	 * 获取本周最后一天
	 * 
	 * @return
	 */
	public static String getLastDayOfWeek() {
		String firstDayOfWeek = getFirstDayOfWeek();
		try {
			Date date = parseString(firstDayOfWeek, PATTERN);
			Calendar c = Calendar.getInstance();
			c.setTime(date);
			c.add(Calendar.DAY_OF_MONTH, 6);
			return new SimpleDateFormat(PATTERN).format(c.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 获取本月第一天
	 * 
	 * @return
	 */
	public static String getFirstDayOfMonth() {
		Calendar c = Calendar.getInstance(Locale.CHINA);
		c.set(Calendar.DAY_OF_MONTH, 1);
		return new SimpleDateFormat(PATTERN).format(c.getTime());
	}

	/**
	 * 获取本月最后一天
	 * 
	 * @return
	 */
	public static String getLastDayOfMonth() {
		Calendar c = Calendar.getInstance(Locale.CHINA);
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
		return new SimpleDateFormat(PATTERN).format(c.getTime());
	}

	public static void main(String[] args) {
		Calendar c1 = Calendar.getInstance();
		c1.set(c1.get(Calendar.YEAR), c1.get(Calendar.MONTH), c1.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		System.out.println(parseDate(c1.getTime(), PATTERN));
		Calendar c2 = Calendar.getInstance();
		c2.set(c2.get(Calendar.YEAR), c2.get(Calendar.MONTH), c2.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		System.out.println(parseDate(c2.getTime(), PATTERN));
		System.out.println(c1.getTime().before(c2.getTime()));

		try {
			Date beginDate = DateUtil.parseString("2016-04-10", DateUtil.PATTERN);
			Date endDate = DateUtil.parseString("2016-04-14", DateUtil.PATTERN);
			Calendar c = Calendar.getInstance();
			c.setTime(beginDate);
			long stepCount = 0L;
			while (beginDate.before(endDate)) {
				String userStepCount = "200";
				if (!StringUtils.isEmpty(userStepCount)) {
					stepCount += Long.parseLong(userStepCount);
				}
				c.add(Calendar.DAY_OF_MONTH, 1);
				beginDate = c.getTime();
			}
			System.out.println(stepCount);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		System.out.println(getFirstDayOfWeek());
		System.out.println(getLastDayOfWeek());
		System.out.println(getFirstDayOfMonth());
		System.out.println(getLastDayOfMonth());
	}
	

	public static final String MONTH_TO_DAY = "MMdd";
	
    public static final String YEAR_TO_DAY = "yyyyMMdd";
    
    public static final String LONG_YEAR_TO_DAY_CN = "yyyy年MM月dd日";
    
    public static final String MONTH_TO_DAY_CN = "MM月dd日";
    
    public static final String LONG_YEAR_TO_DAY = "yyyy-MM-dd";
    
    public static final String YEAR_TO_SECOND = "yyyyMMdd HH:mm:ss";
    
    public static final String LONG_YEAR_TO_SECOND = "yyyy-MM-dd HH:mm:ss"; 

    public static Date timestampToDate(Timestamp value){
        if(value == null){
            return null;
        }
        return new Date(value.getTime()); 
    }
    
    public static Date timestampToDate(Object value){
        return timestampToDate((Timestamp)value);
    }
    
    public static String formatDate(String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date());
    }
    
    public static String formatDate(Date date){
        return formatDate(date, LONG_YEAR_TO_DAY);
    }
    
    public static String formatLongDate(Date date){
        return formatDate(date, LONG_YEAR_TO_SECOND);
    }

    public static String formatDate(Date date, String format) {
        if(date == null) return null;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
	 * 获取当前时间字符串类型
	 * @return
	 */
	public static String getNowDateString(){
		Date date=new Date();
		DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return format.format(date);
	} 
	
    public static String formatTomorrowDate(String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        return sdf.format(calendar.getTime());
    }

    public static int compare(Date date1, Date date2) {
        return date1.compareTo(date2);
    }

    public static int compare(String date1, String format, Date date2)
            throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = sdf.parse(date1);
        return date.compareTo(date2);
    }

    public static Date parse(String date) throws ParseException {
        return parse(date,LONG_YEAR_TO_DAY);
    }
    
    public static Date parse(String date, String format) throws ParseException {
        if(StringUtils.isEmpty(date)) return null;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(date);
    }

    public static Date getNextDate(String hourToSecond) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(YEAR_TO_DAY);
        Date date = sdf.parse(DateUtil.formatDate(YEAR_TO_DAY) + " "
                + hourToSecond);
        return date;
    }

    public static Date getCurrentDateRandomSecond(String hourToSecond)
            throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(YEAR_TO_SECOND);
        String[] fields = hourToSecond.split("\\:");
        Integer randomSecond = RandomUtils.nextInt(59);
        String second = (randomSecond.toString().length() == 1 ? "0"
                + randomSecond : randomSecond + "");
        String date = DateUtil.formatDate(YEAR_TO_DAY) + " " + fields[0] + ":"
                + fields[1] + ":" + second;
        return sdf.parse(date);
    }

    public static Date getNextDateRandomSecond(String hourToSecond)
            throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(YEAR_TO_SECOND);
        String[] fields = hourToSecond.split("\\:");
        Integer randomSecond = RandomUtils.nextInt(59);
        String second = (randomSecond.toString().length() == 1 ? "0"
                + randomSecond : randomSecond + "");
        String date = DateUtil.formatTomorrowDate(YEAR_TO_DAY) + " "
                + fields[0] + ":" + fields[1] + ":" + second;
        return sdf.parse(date);
    }
    
    /** 
    * 按年加,指定日期 
    * @param date 
    * @param value 
    * @return 
    */  
    public static Date addYear(Date date, int value) {  
       Calendar now = Calendar.getInstance();  
       now.setTime(date);  
       now.add(Calendar.YEAR, value);  
       return now.getTime();  
    }  

    public static Date addYear(int value) {  
        Calendar now = Calendar.getInstance();  
        now.add(Calendar.YEAR, value);  
        return now.getTime();  
     }  
   
    /** 
    * 按月加,指定日期 
    *  
    * @param date 
    * @param value 
    * @return 
    */  
    public static Date addMonth(Date date, int value) {  
       Calendar now = Calendar.getInstance();  
       now.setTime(date);  
       now.add(Calendar.MONTH, value);  
       return now.getTime();  
    }  
    
    
    public static Date addMonth(int value) {  
        Calendar now = Calendar.getInstance();  
        now.add(Calendar.MONTH, value);  
        return now.getTime();  
    }  
    
    /** 
    * 按日加,指定日期 
    *  
    * @param date 
    * @param value 
    * @return 
    */  
    public static Date addDay(Date date, int value) {  
       Calendar now = Calendar.getInstance();  
       now.setTime(date);  
       now.add(Calendar.DAY_OF_YEAR, value);  
       return now.getTime();  
    } 
    
    /** 
    * 按日加
    * @param value 
    * @return 
    */  
    public static Date addDay(int value) {  
       Calendar now = Calendar.getInstance();  
       now.add(Calendar.DAY_OF_YEAR, value);  
       return now.getTime();
    }  
    
    public static Date getShortDate(Date date){
    	final String methodName="getShortDate";
        if(date == null) return null;
        try {
            return parse(formatDate(date, LONG_YEAR_TO_DAY), LONG_YEAR_TO_DAY);
        } catch (ParseException e) {
        	LoggerUtil.logError(className, methodName, e);
            return null;
        }
    }
    
    public static Date getShortDate(Date date,String format){
    	final String methodName="getShortDate";
        if(date == null) return null;
        try {
            return parse(formatDate(date, format), format);
        } catch (ParseException e) {
            LoggerUtil.logError(className, methodName, e);
            return null;
        }
    }
    
    public static Date getShortToday(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        
        return cal.getTime();
    }
    
    public static Date getTodayStartDate() throws ParseException{
    	Date date = new Date();
    	String format = formatDate(date, LONG_YEAR_TO_DAY)+" 00:00:00";
    	return parse(format, LONG_YEAR_TO_SECOND);
    }
    
    public static int getAge(Date datumDate,Date birthDate){
    	Calendar datumCd = Calendar.getInstance();
    	datumCd.setTime(datumDate);
    	Calendar birthCd = Calendar.getInstance();
    	birthCd.setTime(birthDate);
    	int datumYear = datumCd.get(Calendar.YEAR);
    	int datumMonth = datumCd.get(Calendar.MONTH);
    	int datumDay = datumCd.get(Calendar.DAY_OF_MONTH);
    	int birthYear = birthCd.get(Calendar.YEAR);
    	int birthMonth = birthCd.get(Calendar.MONTH);
    	int birthDay = birthCd.get(Calendar.DAY_OF_MONTH);
    	
    	int result = datumYear - birthYear - 1;
    	
    	if(datumMonth<birthMonth){
    		return result;
    	}
    	if(datumMonth>birthMonth){
    		return ++result;
    	}
    	if(datumDay>birthDay){
    		return ++result;
    	}
    	return result;
    }
}
